//
//  SelectButtonView.m
//  DesighBook
//
//  Created by 梅明 on 15/9/18.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "SelectButtonView.h"

@implementation SelectButtonView
#define viewwidth         self.frame.size.width

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)addButtonWithButtonArray:(NSArray *)buttonArray AndButtontag:(NSInteger)buttontag
{
    NSInteger tagnum = buttontag/100*100;
    CGFloat buttonwidth = (viewwidth-80)/3;
    for(int i = 0;i<buttonArray.count;i++){
        NSDictionary *dic = buttonArray[i];
        NSNumber *num1 = dic[@"id"];
        NSString *name = dic[@"name"];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i%3*(buttonwidth+20)+20, i/3*50+20, buttonwidth, 30)];
        [button setTitle:name forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.backgroundColor = [UIColor whiteColor];
        button.layer.borderWidth = 0.5f;
        button.layer.borderColor = [[UIColor groupTableViewBackgroundColor]CGColor];
        button.layer.cornerRadius = 5;
        button.tag = tagnum+i;
        if (button.tag == buttontag) {
            button.selected = YES;
        }
        if (button.selected) {
            button.layer.borderColor = [[UIColor redColor]CGColor];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        }
        self.alpha = 1;
        
        //添加点击事件
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }

}

-(void)click:(UIButton *)button
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"postValue" object:button];
}


@end
